<?php

/**
 * Abstrakni modul ktery musi byt predkem vsech trid modulu. Trida zajistuje
 * ziskani hooku pro dany modul.
 *
 * @author David Dusek
 */
namespace CMS;

abstract class AbstractModule {
    /**
     * Instance jadra systemu.
     * @var \CMS\Core
     */
    protected $core;
    
    /**
     * Etity manager.
     * @var Doctrine\ORM\EntityManager 
     */
    protected $em;
    
    public function __construct() {
        $this->core = Core::getInstance();
        $this->em = $this->core->getEm();
    }
    
    /**
     * Projde vsechny metody tridy a ty s prefixem hook, vrati v poli.
     * @return array Pole obsahujici nazvy metod ktere jsou hookem. 
     */
    public static function getHooks() {
        $reflection = new \ReflectionClass(get_called_class());
        $methods = $reflection->getMethods();
        $hooks = array();
        
        if (count($methods) > 0) {
            foreach ($methods as $method) {
                if (self::isHook($method->name)) {
                    $hooks[] = $method->name;
                }
            }
        } 
        
        return $hooks;
    }
    
    /**
     * Overeni jestli metoda je hook.
     * @param String $methodName jmeno metody
     * @return boolean true pokud je metoda hook jinak false
     */
    private static function isHook($methodName) {
        $position = strpos($methodName, "hook");
        
        if ($position !== FALSE && $position == 0) {
            return true;
        }
        return false;
    }
}
